package com.gjj.abox.account;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView;

import com.gjj.abox.BaseActivity;
import com.gjj.abox.account.adapter.CommonAdapter;
import com.gjj.abox.account.viewHolder.CommonViewHolder;
import com.gjj.abox.constant.Constants;
import com.gjj.abox.custom.LoadMoreListView;
import com.gjj.abox.custom.MessageDialog;
import com.gjj.abox.database.R;
import com.gjj.abox.entity.Account;
import com.gjj.abox.login.LoginActivity;
import com.gjj.abox.support.FileChooserActivity;
import com.gjj.abox.utils.BackupTask;
import com.gjj.abox.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;


public class PlatformListActivity extends BaseActivity implements View.OnClickListener{

    private static Boolean isExit = false;
    private LoadMoreListView listView;
    private List<Account> dataList;
    private CommonAdapter adapter;

    private FloatingActionButton fabFirst;
    private int count = 0;
    private String u_name, pp;
    private static final int REQUEST_CODE = 1991;
    private NavigationView nvMainNavigation;
    private DrawerLayout mDrawerLayout;
    private TextView navigationName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        u_name = sharedpreferences.getString("u_name", "");
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDataList();
    }

    private void init() {
        dataList = new ArrayList<>();
        View view = LayoutInflater.from(this).inflate(R.layout.navigation_header,null);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_main_drawer);
        listView = (LoadMoreListView) findViewById(R.id.lmv_platform_list);
        fabFirst = (FloatingActionButton) findViewById(R.id.fab_first);
        nvMainNavigation = (NavigationView) findViewById(R.id.nv_main_navigation);
        nvMainNavigation.addHeaderView(view);
        navigationName = (TextView) view.findViewById(R.id.navigation_header_name);
        navigationName.setText(u_name);
        listView.setAdapter(adapter = new CommonAdapter<Account>(this, dataList, R.layout.platform_item) {
            @Override
            public void convert(CommonViewHolder helper, Account item) {
                helper.setText(R.id.tv_platform_name, item.getPlatformName());
                helper.setText(R.id.tv_platform_time,item.getaSetTime());
            }
        });
        listView.setOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMore();
            }
        });
        listView.setOnItemClickListener(new OnItemClickListener() {
            // 设置item点击监听事件
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // 对空列表判断
                Intent intent = new Intent(PlatformListActivity.this,
                        AccountNameListActivity.class);
                intent.putExtra(Constants.Platform_Name,dataList.get(arg2).getPlatformName());
                startActivity(intent);

            }
        });
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           final int arg2, long arg3) {
                pp = dataList.get(arg2).getPlatformName();
                MessageDialog.showBottomMenu(PlatformListActivity.this, getResources().getStringArray(R.array.arrcontent), new MessageDialog.MenuDialogOnItemClickListener() {
                    @Override
                    public void onItemClick(int index) {
                        switch (index) {
                            case 1:
                                if (boxDb.checkNull(pp, u_name)) {
                                    MessageDialog.showTipsDialog(PlatformListActivity.this, "提示", "该平台您还有记录，请先删除完里面记录");
                                } else {
                                    boxDb.deletePalt(pp, u_name);
                                    dataList.remove(arg2);
                                    adapter.notifyDataSetChanged();
                                }
                                break;
                            case 2:
                                showCustomMessage("修改", pp,true,arg2);
                                break;
                        }
                    }
                });

                return true;
            }

        });
        nvMainNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.nav_home:
                        break;
                    case R.id.nav_messages:
                        break;
                    case R.id.nav_save:
                        MessageDialog.showConfirmDialog(PlatformListActivity.this, getString(R.string.back_up_data_tips), new MessageDialog.DialogConfirmClickListener() {
                            @Override
                            public void confirm() {
                                startActivityForResult(new Intent(PlatformListActivity.this,FileChooserActivity.class).putExtra(Constants.From,Constants.Choose_File_Package),1992);
                            }
                        });
                    case R.id.nav_setting:
                        break;
                    case R.id.nav_about:
                        break;
                }
                mDrawerLayout.closeDrawers(); // 关闭导航菜单
                return false;
            }
        });
        fabFirst.setOnClickListener(this);
    }

    private void getDataList() {
        count = 0;
        if(boxDb.queryPName(u_name, count) == null){
            return;
        }
        dataList.clear();
            dataList.addAll(boxDb.queryPName(u_name, count));
        count = dataList.size();
        adapter.notifyDataSetChanged();
    }

    private void loadMore() {
        listView.onLoadMoreComplete();
        if(boxDb.queryPName(u_name, count) == null){
            return;
        }
        dataList.addAll(boxDb.queryPName(u_name, count));
        count = dataList.size();
        adapter.notifyDataSetChanged();

    }

    private void showCustomMessage(String pTitle, String content, final boolean isEdit,final int position) {
        MessageDialog.showInputDialog(PlatformListActivity.this, pTitle, content, new MessageDialog.InputDialogConfirmClickListener() {
            @Override
            public void confirm(String content) {
                if (isEdit) {
                    if ((!content.equals(""))) {
                        boxDb.updatePlatformName1(content, u_name, pp);
                        boxDb.updatePlatforName2(content, u_name, pp);
                        dataList.get(position).setPlatformName(content);
                        adapter.notifyDataSetChanged();
                    } else {
                        ToastUtil.showShortToast(PlatformListActivity.this, "平台名不可以为空！");
                    }
                } else {
                    if ((!content.equals(""))) {
                        boxDb.insertPaltfor(content, u_name);
                        Intent intent = new Intent(PlatformListActivity.this,
                                AddAccountActivity.class);
                        intent.putExtra(Constants.Edit_Page_Type,Constants.Is_Add);
                        intent.putExtra(Constants.Platform_Name,content);
                        intent.putExtra(Constants.UserName,u_name);
                        startActivity(intent);
                    } else {
                        ToastUtil.showShortToast(PlatformListActivity.this, "平台名不可以为空！");
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fab_first:
                showCustomMessage("添加平台", "",false,-1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1992:
                if(resultCode == RESULT_OK){
                    final String path = data.getStringExtra("file_chooser");
                    MessageDialog.showInputDialog(PlatformListActivity.this, getString(R.string.put_db_name), "", new MessageDialog.InputDialogConfirmClickListener() {
                        @Override
                        public void confirm(String content) {
                            if(content != null && !content.isEmpty()){
                                dataBackUp(path,content);
                            }else{
                                ToastUtil.showShortToast(PlatformListActivity.this, "命名不可以为空！");
                            }
                        }
                    });
                }
                break;
        }
    }

    //数据备份
    private void dataBackUp(String path,String fileName) {
        boxDb.checkUserAccountIsExist("");
        new BackupTask(this, path,fileName).execute("backupDatabase");
    }

}
